package com.hw.controller;

import com.github.pagehelper.PageInfo;
import com.hw.dto.R;
import com.hw.pojo.Categories;
import com.hw.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @ClassName: CategoriesController
 * @Description:
 * @Author: vie0134
 * @Date: 2020-12-15 16:52
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;

    @RequestMapping("/findAllCategories")
    public R findAllCategories(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int limit,
                               String categoryname, String status) {
        PageInfo<Categories> pageInfo = categoriesService.findAllCategories(page, limit, categoryname, status);
        return R.setSuccess(pageInfo);
    }

    @PutMapping("/category")
    public R addCategories(@RequestBody Categories categories) {
        categoriesService.addCategories(categories);
        return R.setSuccess(categories);
    }

    @GetMapping("/detail/{id}")
    public R findCategoriesById(@PathVariable Integer id) {
        Categories categories = categoriesService.findCategoriesById(id);
        return R.setSuccess(categories);
    }

    @PostMapping("/category")
    public R updateCategory(@RequestBody Categories categories) {
        categoriesService.updateCategories(categories);
        return R.setSuccess(null);
    }

    @DeleteMapping("/categories")
    public R deleteCategories(String[] ids) {
        categoriesService.deleteCategoriesByIds(Arrays.asList(ids));
        return R.setSuccess(null);
    }

    @DeleteMapping("/deleteCategoriesById")
    public R deleteCategoriesById(Integer id) {
        categoriesService.deleteCategoriesById(id);
        return R.setSuccess(null);
    }
}
